package com.wuuxiang.polestar.admin.web;

import com.wuuxiang.polestar.admin.dto.BaseReturnDTO;
import com.wuuxiang.polestar.admin.dto.TeamDto;
import com.wuuxiang.polestar.admin.service.team.ITeamService;
import com.wuuxiang.polestar.admin.utils.OvalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 项目名称：polestar        <br>
 * 类描述：                         <br>
 * 创建人：夏鸿鹏                    <br>
 * 创建时间：2017-11-28 12:54        <br>
 * 修改备注：                        <br>
 */

@RestController
public class TeamController {

    @Autowired
    private ITeamService teamService;

    @RequestMapping("/polestar/createTeam")
    @ResponseBody
    public BaseReturnDTO createTeam(@RequestBody TeamDto teamDto) {
        BaseReturnDTO baseReturnDTO = new BaseReturnDTO();
        if (OvalUtils.validate(baseReturnDTO, teamDto)) {
            return baseReturnDTO;
        }
        String nodeName = teamDto.getTeamName().trim();
        if (teamService.checkExists(nodeName)) {
            baseReturnDTO.setReturnCode(-1);
            baseReturnDTO.setErrorCode("500");
            baseReturnDTO.setErrorText("项目组已经存在!");
        } else {
            teamService.createTeam(nodeName, "");
        }
        return baseReturnDTO;
    }

    @RequestMapping("/polestar/removeTeam")
    @ResponseBody
    public BaseReturnDTO removeTeam(@RequestBody TeamDto teamDto) {
        BaseReturnDTO baseReturnDTO = new BaseReturnDTO();
        if (OvalUtils.validate(baseReturnDTO, teamDto)) {
            return baseReturnDTO;
        }
        String nodeName = teamDto.getTeamName().trim();
        if ("default".equals(nodeName)) {
            baseReturnDTO.setReturnCode(-1);
            baseReturnDTO.setErrorCode("500");
            baseReturnDTO.setErrorText("默认项目组不可删除!");
            return baseReturnDTO;
        }
        if (teamService.checkExists(nodeName)) {
            teamService.removeTeam(nodeName);
        }
        return baseReturnDTO;
    }

}
